{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "80018bc3-f3fe-47ae-a579-f837fdf728a0",
   "metadata": {},
   "source": [
    "# Neo4j vector store"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7e67be7b-f135-4feb-827e-6585f86c4ed2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import openai\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY_HERE\"\n",
    "openai.api_key = os.environ[\"OPENAI_API_KEY\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "086f3065-3072-4588-82cb-2a852019451c",
   "metadata": {},
   "source": [
    "## Initiate Neo4j vector wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "910d6b13-576e-47b1-96dd-eacbfe10fa0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.vector_stores import Neo4jVectorStore\n",
    "\n",
    "username = \"neo4j\"\n",
    "password = \"pleaseletmein\"\n",
    "url = \"bolt://localhost:7687\"\n",
    "embed_dim = 1536\n",
    "\n",
    "neo4j_vector = Neo4jVectorStore(username, password, url, embed_dim)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c9c4515-982d-4f78-b099-f70eabfae60c",
   "metadata": {},
   "source": [
    "## Load documents, build the VectorStoreIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "348a4c97-bbf9-4eb1-8669-079c54588fbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index import VectorStoreIndex, SimpleDirectoryReader\n",
    "from IPython.display import Markdown, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aecb970b-7d52-4b0b-8799-605187a01dd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load documents\n",
    "documents = SimpleDirectoryReader(\"../data/paul_graham\").load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8f2ee4d4-addc-49cf-b7ae-0d6146e0f717",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.storage.storage_context import StorageContext\n",
    "\n",
    "storage_context = StorageContext.from_defaults(vector_store=neo4j_vector)\n",
    "index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "59b91a75-0754-4ded-af05-adceda3557d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>At Interleaf, there was a group called Release Engineering that seemed to be as big as the group that actually wrote the software. The software at Interleaf had to be updated on the server, and there was a lot of work involved in maintaining and releasing new versions.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"What happened at interleaf?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e30dd545-7a0e-44a5-aeb7-3eef9312c538",
   "metadata": {},
   "source": [
    "## Load existing vector index\n",
    "\n",
    "In order to connect to an existing vector index, you need to define the `index_name` and `text_node_property` parameters:\n",
    "\n",
    "- index_name: name of the existing vector index (default is `vector`)\n",
    "- text_node_property: name of the property that containt the text value (default is `text`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "872deaed-2fc8-48ba-be52-aae9b260508a",
   "metadata": {},
   "outputs": [],
   "source": [
    "index_name = \"existing_index\"\n",
    "text_node_property = \"text\"\n",
    "existing_vector = Neo4jVectorStore(\n",
    "    username,\n",
    "    password,\n",
    "    url,\n",
    "    embed_dim,\n",
    "    index_name=index_name,\n",
    "    text_node_property=text_node_property,\n",
    ")\n",
    "\n",
    "loaded_index = VectorStoreIndex.from_vector_store(existing_vector)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e286e74-6c3c-43f6-a887-70016740a4f8",
   "metadata": {},
   "source": [
    "## Metadata filtering\n",
    "\n",
    "At the moment, the metadata filtering is not supported."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
